package com.adong.test.onetomany;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @author adong
 */
@NamedQuery(name = "testNamedQuery", query = "from Customer c where c.id=?1")
@Cacheable(true)
@Table(name = "jpa_customer")
@Entity
public class Customer {

    private Integer id;
    private String lastName;
    private String email;
    private Integer age;
    private Set<Order> orders = new HashSet<>();

    public Customer() {
    }

    public Customer(String lastName, Integer age) {
        this.lastName = lastName;
        this.age = age;
    }

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "last_name")
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    /**
     * 映射单向1-n的关联关系
     */
    @JoinColumn(name = "customer_id") //映射外键关系
    @OneToMany//映射一对多关系
    // @OneToMany(cascade ={CascadeType.REMOVE})
    public Set<Order> getOrders() {
        return orders;
    }


    public void setOrders(Set<Order> orders) {
        this.orders = orders;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", lastName='" + lastName + '\'' +
                ", email='" + email + '\'' +
                ", age=" + age +
                '}';
    }
}
